/**
 * Created by myd on 15/10/21.
 */
Ext.define('MyApp.controller.LoginCtrl',{
    extend:'Ext.app.Controller',
    //stores:['LoginStore@MyApp.store'],
    //models:['MyApp.model.base.StaffModel'],
    views: [
        'MyApp.view.LoginView'
    ],
    init:function(){
        var win = WX.Ext.Util.getWin('loginview');
        win.show();
        //this.callParent();
        this.control({
            'loginview button[name=btn-save]': {
                click: this.onLogin
            }
            //添加事件
            //'staffview button[name=btn-add]':{
            //    click:this.AddClick
            //},
            //'staffview button[name=btn-delete]':{
            //    click:this.DeleteClick
            //},
            //'staffview button[name=btn-openstatus]':{
            //    click:this.OpenStatusClick
            //},
            //'staffview button[name=btn-closestatus]':{
            //    click:this.CloseStatusClick
            //}
        });
    },


    //登录事件
    onLogin: function(button) {
        console.log("run");
        var win = button.up('window');
        //取得表单对象
        var form = win.down('form');

        //提交表单内容到后台
        //var formValues=form.getForm().getValues();
        var code_value = form.down('textfield[name=code_value]').getValue();
        var staff_loginname = form.down('textfield[name=staff_loginname]').getValue();
        var staff_password = form.down('textfield[name=staff_password]').getValue();

        //验证表单
        if(!form.isValid()){//表单有错误
        	WX.Ext.Util.showAlertBox("登录信息未填写完整,请填写后登录！");
        	return false;
        }

        Ext.Ajax.request({
            async : false,
            url: 'main/login.action',
            params: {code_value:code_value,staff_loginname:staff_loginname,staff_password:staff_password},
            method: 'POST',
            success: function (response, options) {
                console.log(response.responseText);
                if(null != response.responseText && "" != response.responseText) {
                    var result = Ext.JSON.decode(response.responseText);
                    if (result=="1") {
                        //转码成json格式对象
                        location.href = "main/index.action?cc=" + Ext.Date.now();
                    }else if (result == "2")
                    {
                        Ext.Msg.show({
                            width : 150,
                            title : "错误",
                            msg : "用户名或密码错误，请重试！",
                            buttons : Ext.Msg.OK,
                            icon: Ext.Msg.ERROR
                        });
                    }else if (result == "0")
                    {
                        Ext.Msg.show({
                            width : 150,
                            title : "错误",
                            msg : "验证码错误，请重试！",
                            buttons : Ext.Msg.OK,
                            icon: Ext.Msg.ERROR
                        });
                    }
                }
            },
            failure: function (response, options) {
                Ext.Msg.show({
                    width : 150,
                    title : "错误",
                    msg : "登录时发生错误，请重试！",
                    buttons : Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                });
            }
        });

    },

    /**
     * 按钮添加事件
     * @param button
     * @constructor
     */
    AddClick:function(button){
        //加载供应商编辑控制器
        WX.Ext.Util.loadController(this.application, "MyApp.controller.base.StaffEditCtrl");
        //取得供应商window
        var win = WX.Ext.Util.getWin('staffeditview');
        //设置编辑框可编辑
        //ACBEE.Ext.Util.disableCompoment([win.down('button[name=btn-add]')], win.down('form'), false);
        //清除表单内容
        win.down('form').getForm().reset();
        //设置供应商Id为零
        //win.oldSupplierId = 0;
        //保存当前表格的store
        win.parentStore = button.up('grid').getStore();
        win.setTitle('添加员工信息');
        win.show();
    }
});


// 点击更换验证码
function changeImg(obj) {

    obj.src = "main/code.action" + "?t=" + Math.random();
}